import { DIY_API } from '@/config'
import { request } from '@/utils'
import md5Libs from "uview-ui/libs/function/md5";
import {
	SECRETKEY as baseAppoint
} from "../config/index"

const md5 = md5Libs.md5;
const prefix = 'api/Diylayout/'
const device_token = uni.getStorageSync("devicetoken") || ''



class diyApi {
	static _instance = null
	constructor(){
		serviceApi : DIY_API
	}
	
	static getInstance(){
		if(this._instance instanceof this === false){
			this._instance = new this()
		}
		return this._instance
	}
	
	exec(path, params, method='POST'){
		return new Promise((resolve, reject) => {
		     const api_token = md5(
				`${path}${baseAppoint}`
			)
			uni.request({
				url: `${DIY_API}/${path}?t=${new Date().getTime()}`,
				method: method,
				data: {
					...params,
					api_token,
					client_id: 1,
					// device_token
				},
				success: (res) => {
					const { statusCode, data } = res
					if( statusCode === 200){
						resolve(data)
					}
				},
				fail: (err) => {
					uni.showToast({
						title: `'diy数据请求错误：${err}`,
						icon:'error'
					})
					console.log('diy数据请求错误：',{err})
					reject(err);
				},
			});
		});
	}
	
	getDiyData = (params) => {
 	  return this.exec(`${prefix}getDiylayout`,params)
	}

	getGoodsListByIds = (params) => {
 	  return this.exec('goods/getByIds',params)
	}

}

export default diyApi.getInstance()